﻿using WH.Contracts.Systems.Users;
using WH.Model.OrgContext;
using WH.Model.Systems;
using WH.Repositories.BaseRepositories;
using WH.Repositories.IRepositories.ISystem;
using Rw.Core.Common.Paging;
using WH.Common.Paging;
using SqlSugar;

namespace WH.Repositories.Repositories.System
{
    public class UserRepository : BaseRepository<UserEntity>, IUserRepository
    {
        protected override ISugarQueryable<UserEntity> Query
        {
            get
            {
                return base.Query
               .Includes(b => b.Organization)
               .Includes(b => b.Department)
               .Includes(b => b.Role)
               .Includes(b => b.CardType)
               .Includes(b => b.UserType);
            }
        }
        public UserRepository(ISqlSugarClient dbContext, OrganizationContext organizationContext) : base(dbContext, organizationContext)
        {
        }

        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="pager"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public async Task<IPagedList<UserEntity>> GetUsersByPageAsync(PageParam pager, UserFilter filter)
        {
            var exp = Expressionable.Create<UserEntity>();

            if (!string.IsNullOrWhiteSpace(filter.Keyword))
            {
                filter.Keyword = filter.Keyword.Trim();
                exp.And(user => user.Account.Contains(filter.Keyword) || user.PhoneNumber.Contains(filter.Keyword) || user.Name.Contains(filter.Keyword));
            }

            if (!string.IsNullOrWhiteSpace(filter.Account))
            {
                filter.Account = filter.Account.Trim();
                exp.And(user => user.Account.Contains(filter.Account));
            }

            if (!string.IsNullOrWhiteSpace(filter.PhoneNumber))
            {
                filter.PhoneNumber = filter.PhoneNumber.Trim();
                exp.And(user => user.PhoneNumber.Contains(filter.PhoneNumber));
            }

            if (!string.IsNullOrWhiteSpace(filter.Name))
            {
                filter.Name = filter.Name.Trim();
                exp.And(user => user.Name.Contains(filter.Name));
            }

            if (!string.IsNullOrWhiteSpace(filter.OrganizationId))
            {
                exp.And(user => user.OrganizationId.Equals(filter.OrganizationId));
            }

            if (filter.IsEnable is not null)
            {
                exp.And(user => user.IsEnable == filter.IsEnable.Value);
            }
            if (!string.IsNullOrWhiteSpace(filter.UserTypeCode))
            {
                exp.And(user => user.UserTypeCode == filter.UserTypeCode);
            }
            if (!string.IsNullOrWhiteSpace(filter.RoleId))
            {
                exp.And(user => user.RoleId == filter.RoleId);
            }
            if (!string.IsNullOrWhiteSpace(filter.RoleTypeCode))
            {
                exp.And(user => user.Role.TypeCode == filter.RoleTypeCode);
            }
            if (!string.IsNullOrWhiteSpace(filter.OrganizationId))
            {
                exp.And(user => user.OrganizationId == filter.OrganizationId);
            }

            if (string.IsNullOrWhiteSpace(pager.OrderFileds))
            {
                pager.OrderFileds = nameof(UserEntity.IsAdministrator);
                pager.OrderBymode = OrderByMode.Desc;
            }

            var paged = await PagedFindAllAsync(exp.ToExpression(), pager);
            return paged;
        }

        public async Task<int> AddLogonCount(UserEntity user)
        {
            return await DbContext.Updateable(user).UpdateColumns(x => x.LogonCount).ExecuteCommandAsync();
        }
    }
}
